﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Web.Mvc;
using System.ServiceModel.Syndication;

namespace ThietBiSo.Helpers
{
    public enum FeedType
    {
        Rss,
        Atom
    }

    public class FeedResult : FileResult
    {
        private readonly SyndicationFeed feed;
        private FeedType type;

        public FeedResult(SyndicationFeed feed, FeedType type)
            : base("application/rss+xml")
        {
            this.feed = feed;
            this.type = type;
        }

        protected override void WriteFile(HttpResponseBase response)
        {
            using (XmlWriter writer = XmlWriter.Create(response.OutputStream))
            {
                switch (this.type)
                {
                    case FeedType.Rss:
                        this.feed.GetRss20Formatter().WriteTo(writer);
                        break;
                    case FeedType.Atom:
                        this.feed.GetAtom10Formatter().WriteTo(writer);
                        break;
                }
            }
        }
    }
}